package com.atl.service.impl;

import com.atl.enity.po.StockUser;
import com.atl.enity.web.dto.StockUserSearchDTO;
import com.atl.execption.BusinessException;
import com.atl.mapper.StockUserMapper;
import com.atl.service.IStockUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 手机用户表 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-24
 */
@Service
public class StockUserServiceImpl extends ServiceImpl<StockUserMapper, StockUser> implements IStockUserService {

    /**
     *
     * 查询
     *
     * @param stockUserSearchDTO
     * @return
     */
    @Override
    public Page<StockUser> search(StockUserSearchDTO stockUserSearchDTO) {
        QueryWrapper<StockUser> queryWrapper = new QueryWrapper<>();

        String userUid = stockUserSearchDTO.getUserUid();
        String tel = stockUserSearchDTO.getTel();
        Integer isDisable = stockUserSearchDTO.getIsDisable();
        String operationType = stockUserSearchDTO.getOperationType();
        String area = stockUserSearchDTO.getArea();

        if (StringUtils.isNotBlank(userUid)) {
            queryWrapper.like("user_uid", userUid);
        }

        if (StringUtils.isNotBlank(tel)) {
            queryWrapper.like("tel", tel);
        }

        String startTime = stockUserSearchDTO.getStartTime();
        String endTime = stockUserSearchDTO.getEndTime();
        if (startTime != null && endTime != null) {
            LocalDateTime date1 = LocalDate.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atStartOfDay();
            LocalDateTime date2 = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd")).atTime(LocalTime.MAX);
            queryWrapper.between("create_time", date1, date2);
        }

        if (isDisable != null) {
            queryWrapper.eq("is_disable", isDisable);
        }

        if (StringUtils.isNotBlank(operationType)) {
            queryWrapper.eq("operation_type", operationType);
        }

        if (StringUtils.isNotBlank(area)) {
            queryWrapper.eq("area", area);
        }

        return this.page(new Page<>(stockUserSearchDTO.getCurrent(), stockUserSearchDTO.getPageSize()), queryWrapper);
    }

    /**
     *
     * 登录
     *
     * @param userAccount
     * @param userPassword
     * @param request
     * @return
     */
    @Override
    public StockUser stockUserLogin(String userAccount, String userPassword, HttpServletRequest request) {
        StockUser one = this.lambdaQuery()
                .eq(StockUser::getUserUid, userAccount)
                .eq(StockUser::getPswd, userPassword)
                .one();


        if (one == null) {
            throw new BusinessException(-1, "账号或密码输入错误");
        }

        return one;
    }
}
